package com.pqc.tbk.service.money;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;

import com.pqc.tbk.dao.money.ConfirmMoneyOutDao;
import com.pqc.tbk.dao.user.UserDao;
import com.pqc.tbk.entity.ConstProp;
import com.pqc.tbk.entity.account.User;
import com.pqc.tbk.entity.money.ConfirmMoneyOut;

/**
 * 
 * @author long, create on 2012-8-3 下午8:10:37
 * 
 */
@Controller
@Transactional(readOnly = true)
public class ConfirmOutServiceImpl implements ConfirmOutService {

	private UserDao userDao;
	private ConfirmMoneyOutDao confirmMoneyOutDao;

	@Override
	public User findByUId(final Long id) {
		return userDao.findOne(id, User.class);
	}

	@Override
	@Transactional(readOnly = false)
	public void save(final ConfirmMoneyOut out) {
		confirmMoneyOutDao.save(out);
	}

	@Override
	public boolean applyMoney(final Long uid, final ConfirmMoneyOut out) {
		// 如果已经申请了领款,且状态为未审核,已审核则不能重新发起领款
		if (confirmMoneyOutDao.countByUidAndState(uid) > 0) {
			return false;
		} else {
			final User user = userDao.findOne(uid, User.class);
			out.setUid(uid);
			out.setUname(user.getUsername());
			out.setMoney_rest(user.getMoney_rest());
			out.setState(ConstProp.ConfirmMoneyInout.TO_APPLY);
			out.setApply_time(new Date());
			confirmMoneyOutDao.save(out);
			return true;
		}
	}

	@Autowired
	public void setUserDao(final UserDao userDao) {
		this.userDao = userDao;
	}

	@Autowired
	public void setConfirmMoneyOutDao(
			final ConfirmMoneyOutDao confirmMoneyOutDao) {
		this.confirmMoneyOutDao = confirmMoneyOutDao;
	}

}
